﻿#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.dll"
#r @"..\Microsoft.SharePoint.Client (spo)\Microsoft.SharePoint.Client.Runtime.dll"

#load "Client.fs"

open Client
open Microsoft.SharePoint.Client
open Microsoft.SharePoint.Client.Application
open System.Security
open System.Linq

// =======================================================
// Add custom actions to the ribbon & site settings
// MSDN: http://msdn.microsoft.com/en-us/library/bb802730.aspx
// =======================================================

//================================================
// Add Custom Button calling a JavaScript function
//================================================

let ecbCustomActions = Client.Context.Web.Lists.GetByTitle("Documents").UserCustomActions
Client.Load ecbCustomActions

let ecbCustomAction = ecbCustomActions.Add()
ecbCustomAction.Title <- "MyCustomAction";
ecbCustomAction.Location <- "CommandUI.Ribbon"
ecbCustomAction.Sequence <- 1001
ecbCustomAction.Url <- "javascript: alert('Test successfull');"

ecbCustomAction.Update()
Client.Load ecbCustomAction


//===============================================
// Hiding Ribbon buttons
//===============================================

let libraryActions = Client.Context.Web.Lists.GetByTitle("Documents").UserCustomActions
Client.Load libraryActions

for ca in libraryActions do
    Client.Load ca
    let msg = ca.Title + " " + ca.CommandUIExtension
    ca.DeleteObject()
    Client.ExecuteQuery()
    printfn "%s\n" msg
    
let action = libraryActions.Add()
action.Title <- "RemoveExplorerButton"
action.Location <- "Ribbon.CustomSharingTab.Edit.Requests.Controls._children"
action.Sequence <- 10021

let commandUIExten = new System.Text.StringBuilder()
commandUIExten.Append("<CommandUIExtension>")
commandUIExten.Append("<CommandUIDefinitions>")
commandUIExten.Append("<CommandUIDefinition Location=\"Ribbon.Library.Actions.OpenWithExplorer\" />")
commandUIExten.Append("</CommandUIDefinitions>")
commandUIExten.Append("</CommandUIExtension>")
action.CommandUIExtension <- commandUIExten.ToString()
action.Update()
Client.ExecuteQuery()


//================================================
// Add Custom Button calling a JavaScript function
//================================================

let customActions = Client.Context.Web.Lists.GetByTitle("Documents").UserCustomActions
Client.Load customActions

let linkAction = customActions.Add();
linkAction.Title <- "New Link";
linkAction.Location <- "CommandUI.Ribbon";
linkAction.Url <- "/_layouts/15/NewLink.aspx?List=%7B84EB95E3%2D104D%2D4FA7%2D985E%2DEEF09B0841DD%7D&RootFolder=%2FDocuments&ContentTypeId=0x01010A0039E59CCEFAC59B4AAE4CA9388F3D8AC400BCE7A4672162B941BD3A3CBC417AF4BA"
linkAction.Sequence <- 10001;
                
let linkCommandUI = new System.Text.StringBuilder();
linkCommandUI.Append("<CommandUIExtension>");
linkCommandUI.Append("<CommandUIDefinitions><CommandUIDefinition Location=\"Ribbon.Documents.New.Controls._children\">");
linkCommandUI.Append("<Button Id=\"Ribbon.ListItem.Actions.NewLink\" ");
linkCommandUI.Append("Alt=\"New Link\" Command=\"NewLink\" ");
linkCommandUI.Append("LabelText=\"New Link\" TemplateAlias=\"o1\" ");
linkCommandUI.Append("Image32by32=\"https://geek.sharepoint.com/StyleLibraries/NewLink_32x32.gif\" ");
linkCommandUI.Append("Image16by16=\"https://geek.sharepoint.com/StyleLibraries/NewLink_16x16.gif\" />");
linkCommandUI.Append(@"</CommandUIDefinition></CommandUIDefinitions>");
linkCommandUI.Append(@"<CommandUIHandlers>");
let linkCommand = new System.Text.StringBuilder();
linkCommand.Append(@"javascript: window.open('/_layouts/15/NewLink.aspx?List=%7B84EB95E3%2D104D%2D4FA7%2D985E%2DEEF09B0841DD%7D&amp;RootFolder=%2Fsites%2F000161%2FDocuments&amp;ContentTypeId=0x01010A0039E59CCEFAC59B4AAE4CA9388F3D8AC400BCE7A4672162B941BD3A3CBC417AF4BA', '_top');");
linkCommandUI.Append("<CommandUIHandler Command=\"NewLink\" CommandAction=\"" + linkCommand.ToString() + "\" />");
linkCommandUI.Append(@"</CommandUIHandlers>");
linkCommandUI.Append(@"</CommandUIExtension>");

linkAction.CommandUIExtension <- linkCommandUI.ToString();
linkAction.Update();

Client.Context.Load(linkAction)
Client.ExecuteQuery()

//================================================
// Add a custom action to the site settings
//================================================

let siteSettings = Client.Context.Web.UserCustomActions
Client.Load siteSettings
let newSetting = siteSettings.Add()
newSetting.Location <- "Microsoft.SharePoint.SiteSettings"
newSetting.Group <- "Customization"
newSetting.Sequence <- 101
newSetting.Title <- "Website User Custom Action"
newSetting.Url <- "/TestPages/TestPage.aspx"
newSetting.Update()
Client.Context.Web.Update()
Client.ExecuteQuery()

//========================
//== Button with dialog ==
//========================

let baseAction = libraryActions.Add();
baseAction.Title <- "New Baseline";
baseAction.Location <- "CommandUI.Ribbon";
baseAction.Sequence <- 10001;
                
let commandUIExt = new System.Text.StringBuilder();
commandUIExt.Append("<CommandUIExtension>");
commandUIExt.Append("<CommandUIDefinitions><CommandUIDefinition Location=\"Ribbon.CustomSharingTab.Edit.Controls._children\">");
commandUIExt.Append("<Button Id=\"Ribbon.CustomSharingTab.Requests.NewBaseline\" ");
commandUIExt.Append("Alt=\"New Baseline\" Command=\"NewBaseline\" ");
commandUIExt.Append("LabelText=\"New Baseline\" TemplateAlias=\"o1\" ");
commandUIExt.Append("Image32by32=\"https://geek.sharepoint.com/StyleLibraries/NewBaseline_32x32.gif\" ");
commandUIExt.Append("Image16by16=\"https://geek.sharepoint.com/StyleLibraries/NewBaseline_16x16.gif\" />");
commandUIExt.Append(@"</CommandUIDefinition></CommandUIDefinitions>");
commandUIExt.Append(@"<CommandUIHandlers>");

let command = new System.Text.StringBuilder();
command.Append(@"javascript: function CallDETCustomDialog(dialogResult, returnValue)");
command.Append(@"{SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);}");
command.Append(@"var options = {");
command.Append(@"url: 'https://geek.sharepoint.com/_layouts/15/NewDocSet.aspx?List=84eb95e3%2D104d%2D4fa7%2D985e%2Deef09b0841dd&amp;ContentTypeId=0x0120D52000F0CAD2C804E68147BB7D0EF6BA2179730028E91DFDDD5964428AFF76BB36D4A203&amp;RootFolder=%2Fsites%2F000161%2FDocuments',");
command.Append(@"title: 'New Baseline',");
command.Append(@"allowMaximize: false, showClose: true,");
command.Append(@"width: 500, height: 250,");
command.Append(@"dialogReturnValueCallback: CallDETCustomDialog };");
command.Append(@"SP.UI.ModalDialog.showModalDialog(options);");

commandUIExten.Append("<CommandUIHandler Command=\"NewBaseline\" CommandAction=\"" + command.ToString() + "\" />");
commandUIExten.Append(@"</CommandUIHandlers>");
commandUIExten.Append(@"</CommandUIExtension>");

baseAction.CommandUIExtension <- commandUIExten.ToString();
baseAction.Update();

Client.Context.Load(baseAction)
Client.ExecuteQuery()

//========================
// Add a complex ribbon 
//========================

let extend = "<CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
          Location=\"Ribbon.Tabs._children\">
          <Tab
            Id=\"Ribbon.CustomRibbonTab\"
            Title=\"Custom Tab\"
            Description=\"Custom Tab Description\"
            Sequence=\"501\">
            <Scaling
              Id=\"Ribbon.CustomRibbonTab.Scaling\">
              <MaxSize
                Id=\"Ribbon.CustomRibbonTab.MaxSize\"
                GroupId=\"Ribbon.CustomRibbonTab.ManageCustomGroup\"
                Size=\"OneLargeTwoMedium\"/>
              <MaxSize
                Id=\"Ribbon.CustomRibbonTab.TabTwoMaxSize\"
                GroupId=\"Ribbon.CustomRibbonTab.NewOpenCustomGroup\"
                Size=\"TwoLarge\" />
              <Scale
                Id=\"Ribbon.CustomRibbonTab.Scaling.CustomTabScaling\"
                GroupId=\"Ribbon.CustomRibbonTab.ManageCustomGroup\"
                Size=\"OneLargeTwoMedium\" />
              <Scale
                Id=\"Ribbon.CustomRibbonTab.Scaling.CustomSecondTabScaling\"
                GroupId=\"Ribbon.CustomRibbonTab.NewOpenCustomGroup\"
                Size=\"TwoLarge\" />
            </Scaling>
            <Groups Id=\"Ribbon.CustomRibbonTab.Groups\">
              <Group
                Id=\"Ribbon.CustomRibbonTab.ManageCustomGroup\"
                Description=\"Group to Custom Functions\"
                Title=\"Manage Item\"
                Sequence=\"52\"
                Template=\"Ribbon.Templates.CustomTemplate\">
                <Controls Id=\"Ribbon.CustomRibbonTab.ManageCustomGroup.Controls\">
                  <Button
                    Id=\"Ribbon.CustomRibbonTab.ManageCustomGroup.Accept\"
                    Command=\"CustomRibbonTab.AcceptCustomCommand\"
                    Sequence=\"15\"
                    Image32by32=\"{SiteUrl}/_layouts/15/1033/Images/formatmap32x32.png?rev=23\"
                    Image32by32Top=\"-68\"
                    Image32by32Left=\"-272\"
                    Description=\"Accept Item\"
                    LabelText=\"Accept\"
                    TemplateAlias=\"AWR\" />
                  <Button
                    Id=\"Ribbon.CustomRibbonTab.ManageCustomGroup.Reject\"
                    Command=\"CustomRibbonTab.RejectCustomCommand\"
                    Sequence=\"17\"
                    Image16by16=\"{SiteUrl}/_layouts/15/1033/Images/formatmap16x16.png?rev=23\"
                    Image16by16Top=\"-216\"
                    Image16by16Left=\"-290\"
                    Description=\"Reject Item\"
                    LabelText=\"Reject\"
                    TemplateAlias=\"RWR\"/>
                  <Button
                    Id=\"Ribbon.CustomRibbonTab.ManageCustomGroup.Verify\"
                    Command=\"CustomRibbonTab.VerifyCustomCommand\"
                    Sequence=\"19\"
                    Image16by16=\"{SiteUrl}/_layouts/15/1033/Images/formatmap16x16.png?rev=23\"
                    Image16by16Top=\"-126\"
                    Image16by16Left=\"-144\"
                    Description=\"Verify Item\"
                    LabelText=\"Verify\"
                    TemplateAlias=\"ACWR\"/>
                  <Button
                   Id=\"Ribbon.CustomRibbonTab.ManageCustomGroup.Close\"
                   Command=\"CustomRibbonTab.CloseCustomCommand\"
                   Sequence=\"19\"
                   Image32by32=\"{SiteUrl}/_layouts/15/1033/Images/formatmap32x32.png?rev=23\"
                   Image32by32Top=\"-0\"
                   Image32by32Left=\"-34\"
                   Description=\"Close Item\"
                   LabelText=\"Close\"
                   TemplateAlias=\"CWR\"/>
                  <Button
                   Id=\"Ribbon.CustomRibbonTab.ManageCustomGroup.Copy\"
                   Command=\"CustomRibbonTab.CopyCustomCommand\"
                   Sequence=\"19\"
                   Image32by32=\"{SiteUrl}/_layouts/15/1033/Images/formatmap32x32.png?rev=23\"
                   Image32by32Top=\"-442\"
                   Image32by32Left=\"-67\"
                   Description=\"Copy Item\"
                   LabelText=\"Copy\"
                   TemplateAlias=\"CPWR\"/>
                </Controls>
              </Group>
              <Group
                Id=\"Ribbon.CustomRibbonTab.NewOpenCustomGroup\"
                Description=\"Group to manage item\"
                Title=\"New &amp; Open\"
                Sequence=\"53\"
                Template=\"Ribbon.Templates.CustomTemplate\">
                <Controls Id=\"Ribbon.CustomRibbonTab.NewOpenCustomGroup.Controls\">
                  <Button
                    Id=\"Ribbon.CustomRibbonTab.NewOpenCustomGroup.New\"
                    Command=\"CustomRibbonTab.NewCustomCommand\"
                    Sequence=\"15\"
                    Image32by32=\"{SiteUrl}/_layouts/15/1033/Images/formatmap32x32.png?rev=23\"
                    Image32by32Top=\"-33\"
                    Image32by32Left=\"-66\"
                    Description=\"New Item\"
                    LabelText=\"New\"
                    TemplateAlias=\"LOR\"/>
                  <Button
                   Id=\"Ribbon.CustomRibbonTab.NewOpenCustomGroup.Open\"
                   Command=\"CustomRibbonTab.OpenCustomCommand\"
                   Sequence=\"15\"
                   Image32by32=\"{SiteUrl}/_layouts/15/1033/Images/formatmap32x32.png?rev=23\"
                   Image32by32Top=\"-170\"
                   Image32by32Left=\"-138\"
                   Description=\"Open Item\"
                   LabelText=\"Open\"
                   TemplateAlias=\"LORS\"/>
                </Controls>
              </Group>
            </Groups>
          </Tab>
        </CommandUIDefinition>
        <CommandUIDefinition Location=\"Ribbon.Templates._children\">
          <GroupTemplate Id=\"Ribbon.Templates.CustomTemplate\">
            <Layout
              Title=\"OneLargeTwoMedium\"
              LayoutTitle=\"OneLargeTwoMedium\">
              <Section Alignment=\"Top\" Type=\"OneRow\">
                <Row>
                  <ControlRef DisplayMode=\"Large\" TemplateAlias=\"AWR\" />
                </Row>
              </Section>
              <Section Alignment=\"Top\" Type=\"TwoRow\">
                <Row>
                  <ControlRef DisplayMode=\"Medium\" TemplateAlias=\"RWR\" />
                </Row>
                <Row>
                  <ControlRef DisplayMode=\"Medium\" TemplateAlias=\"ACWR\" />
                </Row>
              </Section>
              <Section Alignment=\"Top\" Type=\"OneRow\">
                <Row>
                  <ControlRef DisplayMode=\"Large\" TemplateAlias=\"CWR\" />
                </Row>
              </Section>
              <Section Alignment=\"Top\" Type=\"OneRow\">
                <Row>
                  <ControlRef DisplayMode=\"Large\" TemplateAlias=\"CPWR\" />
                </Row>
              </Section>
            </Layout>
            <Layout
             Title=\"TwoLarge\"
             LayoutTitle=\"TwoLarge\">
              <Section Alignment=\"Top\" Type=\"OneRow\">
                <Row>
                  <ControlRef DisplayMode=\"Large\" TemplateAlias=\"LOR\" />
                </Row>
              </Section>
              <Section Alignment=\"Top\" Type=\"OneRow\">
                <Row>
                  <ControlRef DisplayMode=\"Large\" TemplateAlias=\"LORS\" />
                </Row>
              </Section>
            </Layout>
          </GroupTemplate>
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
          Command=\"CustomRibbonTab.AcceptCustomCommand\"
          CommandAction=\"javascript:GetCurrentItem('AP');\"/>
        <CommandUIHandler
          Command=\"CustomRibbonTab.RejectCustomCommand\"
          CommandAction=\"javascript:GetCurrentItem('RJ');\"/>
        <CommandUIHandler
          Command=\"CustomRibbonTab.VerifyCustomCommand\"
          CommandAction=\"javascript:GetCurrentItem('AK');\"/>
        <CommandUIHandler
          Command=\"CustomRibbonTab.NewCustomCommand\"
          CommandAction=\"javascript:AddNewCustom();\"/>
        <CommandUIHandler
          Command=\"CustomRibbonTab.OpenCustomCommand\"
          CommandAction=\"javascript:OpenExistingCustom();\"/>
        <CommandUIHandler
          Command=\"CustomRibbonTab.CloseCustomCommand\"
          CommandAction=\"javascript:CloseExistingCustom();\"/>
        <CommandUIHandler
          Command =\"CustomRibbonTab.CopyCustomCommand\"
          CommandAction=\"Javascript:CopyCustom();\"/>
      </CommandUIHandlers>
    </CommandUIExtension>"

let ca = libraryActions.Add()
ca.Title <- "HalloWelt";
ca.Location <- "CommandUI.Ribbon";
ca.Sequence <- 10022
ca.CommandUIExtension <- extend
ca.Update()
Client.Context.Load(ca)
Client.ExecuteQuery()
